package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dto.CommodityDto;
import com.woniuxy.dto.GoodsPropDto;
import com.woniuxy.dto.UserRelatedDto.UserTableDetails;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.entity.*;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.CommodityException;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.mapper.CommodityTableMapper;
import com.woniuxy.param.AddCommodityParam;
import com.woniuxy.param.BaseParam;
import com.woniuxy.param.CommodityParam;
import com.woniuxy.service.CommodityTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.GoodsGeneralService;
import com.woniuxy.service.GoodsSmallService;
import com.woniuxy.service.GoodsSmallerService;
import com.woniuxy.util.DateUtil;
import com.woniuxy.util.PageMysqlUtil;
import com.woniuxy.util.WoniuBeanUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.lang.System.currentTimeMillis;

/**
 * <p>
 * 商品详情
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@Service
public class CommodityTableServiceImpl extends ServiceImpl<CommodityTableMapper, CommodityTable> implements CommodityTableService {
    @Resource
    private GoodsGeneralService goodsGeneralService;
    @Resource
    private GoodsSmallService goodsSmallService;
    @Resource
    private GoodsSmallerService goodsSmallerService;


    @Override
    public BaseDto<CommodityDto> listCommodityByKey(CommodityParam cp) {

        QueryWrapper<CommodityTable> query = new QueryWrapper<>();
        //请输入商品名称、分类名称进行查询
        if (!StringUtils.isEmpty(cp.getKey())) {
            query.and(wrapper -> wrapper
                    .like("goods_table_name", cp.getKey())
                    .or()
                    .like("commodity_table_name", cp.getKey())
            );
        }
        //评估状态
        if (!StringUtils.isEmpty(cp.getStatus())) {
            query.eq("goods_process", cp.getStatus());
        }
        //开始时间
        if (!StringUtils.isEmpty(cp.getStartTime())) {
            query.ge("user_inputtime", cp.getStartTime());
        }
        //结束时间
        if (!StringUtils.isEmpty(cp.getEndTime())) {
            query.le("user_inputtime", cp.getEndTime());
        }
        //结束时间
        if (!StringUtils.isEmpty(cp.getOperator())) {
            query.le("user_Input", cp.getOperator());
        }
        //商品状态
        query.eq("warehouse_display_status", 0);
        BaseDto<CommodityDto> dto = PageMysqlUtil.pageQuery(CommodityDto.class, this, query, new BaseParam(cp.getIndexPage(), cp.getPageSize()));

        return dto;
    }

    /**
     * 添加商品信息
     *
     * @param addCommodityParam
     * @return
     */
    @Override
    public CommodityDto addCommodity(AddCommodityParam addCommodityParam) {
//        Integer id = addCommodityParam.getCommodityTableId();
//        CommodityTable commodityDB = getById(id);
//
//        if (commodityDB != null) throw new CommodityException("商品信息已存在",10001);


        UserTableDetails userTableDetails = (UserTableDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        addCommodityParam.setUserCreator(userTableDetails.getUserName());
        addCommodityParam.setGoodsProcess(1);
        addCommodityParam.setWarehouseDisplayStatus(0);
        //获取3类id拼接新的名字

        GoodsGeneral goodsGeneral = goodsGeneralService.getById(addCommodityParam.getGoodsBrandsId());
        GoodsSmall goodsSmall = goodsSmallService.getById(addCommodityParam.getGoodsSmallId());
        GoodsSmaller smaller = goodsSmallerService.getById(addCommodityParam.getGoodsSmallerId());
        String time = DateUtil.parseDateLong(currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        addCommodityParam.setUserInputtime(time);
        addCommodityParam.setCreatorTime(time);
        CommodityTable commodityTable = WoniuBeanUtils.copyObject(addCommodityParam, CommodityTable.class);
        String name = goodsGeneral.getGoodsGeneralName() + ">" + goodsSmall.getGoodsSmallName() + ">" + smaller.getGoodsSmallerName();
        commodityTable.setCommodityTableName(name);
        commodityTable.setWarehouseCount(1);

        //获取3种属性组
        String gpropsList1 = addCommodityParam.getGpropsList1();
        String gpropsList2 = addCommodityParam.getGpropsList2();
        List<String> gpropsList3 = addCommodityParam.getGpropsList3();
        gpropsList3.add(gpropsList1);
        gpropsList3.add(gpropsList2);
        JSONArray jsonArray = null;

        jsonArray = new JSONArray(gpropsList3);

        commodityTable.setModifiedBy(userTableDetails.getUserName());
        commodityTable.setModifiedTime(time);
        commodityTable.setGoodsPropList(jsonArray.toString());
        if (save(commodityTable)) return WoniuBeanUtils.copyObject(commodityTable, CommodityDto.class);
        throw new CommodityException("新增商品信息失败", 11111);
    }


    @Override
    public CommodityDto getCommodityInfo(Integer commodityTableId) {
        CommodityTable one = getOne(new QueryWrapper<CommodityTable>()
                .eq("commodity_table_id", commodityTableId)
                .eq("warehouse_display_status", 0));
        return WoniuBeanUtils.copyObject(one, CommodityDto.class);
    }

    @Override
    public boolean removeGoodsCommodityByIds(List<Integer> ids) {
        List<CommodityTable> commodityTables = listByIds(ids);
        if (ids.size() != commodityTables.size())
            throw new PawnUserException(CodeEnum.COMMODITY_DELETE_FAIL_2.getMessage(), CodeEnum.COMMODITY_DELETE_FAIL_2.getCode());
        for (CommodityTable table : commodityTables) {
            table.setWarehouseDisplayStatus(1);
        }
        return updateBatchById(commodityTables);
    }


}
